package com.cc.wechat.mq.config;

import com.cc.wechat.mq.constant.RabbitMQConstants;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * RabbitMQ配置类
 */
@Configuration
public class RabbitMQConfig {

    /**
     * 配置RabbitTemplate
     */
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory, MessageConverter messageConverter) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(messageConverter);
        return rabbitTemplate;
    }

    /**
     * 配置死信交换机
     */
    @Bean
    public DirectExchange deadLetterExchange() {
        return new DirectExchange(RabbitMQConstants.DEAD_LETTER_EXCHANGE);
    }

    /**
     * 配置死信队列
     */
    @Bean
    public Queue deadLetterQueue() {
        return QueueBuilder.durable(RabbitMQConstants.DEAD_LETTER_QUEUE).build();
    }

    /**
     * 绑定死信队列到死信交换机
     */
    @Bean
    public Binding deadLetterBinding() {
        return BindingBuilder.bind(deadLetterQueue())
                .to(deadLetterExchange())
                .with(RabbitMQConstants.DEAD_LETTER_ROUTING_KEY);
    }

    /**
     * 配置聊天交换机
     */
    @Bean
    public DirectExchange chatExchange() {
        return new DirectExchange(RabbitMQConstants.CHAT_EXCHANGE);
    }

    /**
     * 配置通知交换机
     */
    @Bean
    public DirectExchange notificationExchange() {
        return new DirectExchange(RabbitMQConstants.NOTIFICATION_EXCHANGE);
    }

    /**
     * 配置私聊消息队列
     */
    @Bean
    public Queue privateMessageQueue() {
        Map<String, Object> args = new HashMap<>();
        // 消息过期后转发到死信交换机
        args.put("x-dead-letter-exchange", RabbitMQConstants.DEAD_LETTER_EXCHANGE);
        args.put("x-dead-letter-routing-key", RabbitMQConstants.DEAD_LETTER_ROUTING_KEY);
        // 消息过期时间，24小时
        args.put("x-message-ttl", 86400000);
        return QueueBuilder.durable(RabbitMQConstants.PRIVATE_MESSAGE_QUEUE)
                .withArguments(args)
                .build();
    }

    /**
     * 配置群聊消息队列
     */
    @Bean
    public Queue groupMessageQueue() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange", RabbitMQConstants.DEAD_LETTER_EXCHANGE);
        args.put("x-dead-letter-routing-key", RabbitMQConstants.DEAD_LETTER_ROUTING_KEY);
        args.put("x-message-ttl", 86400000);
        return QueueBuilder.durable(RabbitMQConstants.GROUP_MESSAGE_QUEUE)
                .withArguments(args)
                .build();
    }

    /**
     * 配置好友请求队列
     */
    @Bean
    public Queue friendRequestQueue() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange", RabbitMQConstants.DEAD_LETTER_EXCHANGE);
        args.put("x-dead-letter-routing-key", RabbitMQConstants.DEAD_LETTER_ROUTING_KEY);
        args.put("x-message-ttl", 604800000); // 7天过期
        return QueueBuilder.durable(RabbitMQConstants.FRIEND_REQUEST_QUEUE)
                .withArguments(args)
                .build();
    }

    /**
     * 配置好友响应队列
     */
    @Bean
    public Queue friendResponseQueue() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange", RabbitMQConstants.DEAD_LETTER_EXCHANGE);
        args.put("x-dead-letter-routing-key", RabbitMQConstants.DEAD_LETTER_ROUTING_KEY);
        args.put("x-message-ttl", 604800000); // 7天过期
        return QueueBuilder.durable(RabbitMQConstants.FRIEND_RESPONSE_QUEUE)
                .withArguments(args)
                .build();
    }

    /**
     * 绑定私聊消息队列到聊天交换机
     */
    @Bean
    public Binding privateMessageBinding() {
        return BindingBuilder.bind(privateMessageQueue())
                .to(chatExchange())
                .with(RabbitMQConstants.PRIVATE_MESSAGE_ROUTING_KEY);
    }

    /**
     * 绑定群聊消息队列到聊天交换机
     */
    @Bean
    public Binding groupMessageBinding() {
        return BindingBuilder.bind(groupMessageQueue())
                .to(chatExchange())
                .with(RabbitMQConstants.GROUP_MESSAGE_ROUTING_KEY);
    }

    /**
     * 绑定好友请求队列到通知交换机
     */
    @Bean
    public Binding friendRequestBinding() {
        return BindingBuilder.bind(friendRequestQueue())
                .to(notificationExchange())
                .with(RabbitMQConstants.FRIEND_REQUEST_ROUTING_KEY);
    }

    /**
     * 绑定好友响应队列到通知交换机
     */
    @Bean
    public Binding friendResponseBinding() {
        return BindingBuilder.bind(friendResponseQueue())
                .to(notificationExchange())
                .with(RabbitMQConstants.FRIEND_RESPONSE_ROUTING_KEY);
    }
} 